<!-- 账号密码登录页 -->
<template>
	<view class="uni-content">
		<!-- 顶部文字 -->
		<text class="title title-box">账号密码登录</text>
		<uni-forms>
			<uni-forms-item name="username">
				<uni-easyinput :focus="focusUsername" @blur="focusUsername = false" class="input-box" :inputBorder="false" v-model="username" placeholder="请输入手机号/用户名" />
			</uni-forms-item>
			<uni-forms-item name="password">
				<uni-easyinput :focus="focusPassword" @blur="focusPassword = false" class="input-box" clearable type="password" :inputBorder="false" v-model="password"
					placeholder="请输入密码" />
			</uni-forms-item>
		</uni-forms>
		<uni-captcha v-if="needCaptcha" focus ref="captcha" scene="login-by-pwd" v-model="captcha" />
		<!-- 带选择框的隐私政策协议组件 -->
		<uni-id-pages-agreements scope="login" ref="agreements" ></uni-id-pages-agreements>
		<button class="uni-btn" type="primary" @click="pwdLogin">登录</button>
		<!-- 忘记密码 -->
		<!-- <view class="link-box">
			<text class="link" @click="toRegister">注册账号</text>
		</view>  -->
		<!-- 悬浮登录方式组件 -->
	<!-- 	<uni-id-pages-fab-login ref="uniFabLogin"></uni-id-pages-fab-login> -->
	</view>
</template>

<script>
	import mixin from '@/subunimodules/uni-id-pages/common/login-page.mixin.js';
	import cloudImage from '@/subunimodules/uni-id-pages/components/cloud-image/cloud-image.vue';
	import uniIdPagesAgreements from '@/subunimodules/uni-id-pages/components/uni-id-pages-agreements/uni-id-pages-agreements.vue';
	import uniIdPagesAvatar from '@/subunimodules/uni-id-pages/components/uni-id-pages-avatar/uni-id-pages-avatar.vue';
	import uniIdPagesBindMobile from '@/subunimodules/uni-id-pages/components/uni-id-pages-bind-mobile/uni-id-pages-bind-mobile.vue';
	import uniIdPagesFabLogin from '@/subunimodules/uni-id-pages/components/uni-id-pages-fab-login/uni-id-pages-fab-login.vue';
	import uniIdPagesInvForm from '@/subunimodules/uni-id-pages/components/uni-id-pages-inv-form/uni-id-pages-inv-form.vue';
	import uniIdPagesSmsForm from '@/subunimodules/uni-id-pages/components/uni-id-pages-sms-form/uni-id-pages-sms-form.vue';
	import uniIdPagesUserProfile from '@/subunimodules/uni-id-pages/components/uni-id-pages-user-profile/uni-id-pages-user-profile.vue';
	
	const uniIdCo = uniCloud.importObject("uni-id-co",{
		errorOptions:{
			type:'toast'
		}
	})
	export default {
		components: {
			cloudImage,
			uniIdPagesAgreements,
			uniIdPagesAvatar,
			uniIdPagesBindMobile,
			uniIdPagesFabLogin,
			uniIdPagesInvForm,
			uniIdPagesSmsForm,
			uniIdPagesUserProfile
		},
		mixins: [mixin],
		data() {
			return {
				"password": "",
				"username": "",
				"captcha": "",
				"needCaptcha": false,
				"focusUsername":false,
				"focusPassword":false
			}
		},
		onShow() {
			// #ifdef H5
			document.onkeydown = event => {
				var e = event || window.event;
				if (e && e.keyCode == 13) { //回车键的键值为13
					this.pwdLogin()
				}
			};
			// #endif
		},
		methods: {
			// 页面跳转，找回密码
			toRetrievePwd() {
				let url = '/subunimodules/uni-id-pages/pages/retrieve/retrieve'
				//如果刚好用户名输入框的值为手机号码，就把它传到retrieve页面，根据该手机号找回密码
				if (/^1\d{10}$/.test(this.username)) {
					url += `?phoneNumber=${this.username}`
				}
				uni.navigateTo({
					url
				})
			},
			/**
			 * 密码登录
			 */
			pwdLogin() {
				if(!this.password.length){
					this.focusPassword = true
					return uni.showToast({
						title: '请输入密码',
						icon: 'none'
					});
				}
				if(!this.username.length){
					this.focusUsername = true
					return uni.showToast({
						title: '请输入手机号/用户名',
						icon: 'none'
					});
				}
				 
				
				if (this.needAgreements && !this.agree) {
					return this.$refs.agreements.popup(this.pwdLogin)
				}
				
				let data = {
					"password": this.password,
					"captcha": this.captcha
				}
				
				var that = this
				that.$http('user.pwsLogin', {
					username: this.username,
					password:this.password
				}).then(res => {
					// console.info(JSON.stringify(res))
					if (res.code === 1) {
						uni.setStorageSync("UId", res.user.id);
						//uni.setStorageSync("UId",3004);
						uni.setStorageSync("token", res.user.id)
						uni.setStorageSync("mobile", res.user.mobile)
						uni.setStorageSync("isLogin", true)
						uni.setStorageSync("InvitationCode", res.InvitationCode)
						uni.navigateBack({
							delta: 2
						})
					}
					else{
						uni.showToast({
							title:"请核对账户密码",
							icon:null
						})
					}
					 
				});
			},
			/* 前往注册 */
			toRegister(e) {
				console.log(e);
				uni.navigateTo({
					url: '/subunimodules/uni-id-pages/pages/register/register'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import "@/subunimodules/uni-id-pages/common/login-page.scss";

	.forget{
		font-size: 12px;
		color: #8a8f8b;
	}

	.link-box {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: space-between;
		margin-top: 20px;
	}

	.link {
		font-size: 12px;
	}
</style>
